import http from '@/common/vmeitime-http/interface'

// 获取项目列表
export const getProjectList = async (params = {}) => {
  return http.post('project/getProjectList', params)
}

// 新增项目接口
export const addProject = async (params = {}) => {
  return http.post('project/addProject', params)
}

// 修改项目接口
export const editProject = async (params = {}) => {
  return http.post('project/editProject', params)
}

// 获取项目详情
export const getProjectDetail = async (params = {}) => {
  return http.post('project/getProjectDetail', params)
}

// 获取跟进项目列表
export const getFollowProjectList = async (params = {}) => {
  return http.post('project/getFollowProjectList', params)
}

// 上传单张图片
export const uploadImage = async (filePath) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: 'https://data.web1995.top/api/common/upload',
      filePath,
      name: 'file',
      success: (uploadRes) => {
        try {
          const res = JSON.parse(uploadRes.data);
          if (res.code === 1 && res.data && res.data.fullurl) {
            resolve(res.data.fullurl);
          } else {
            reject(res.msg || '上传失败');
          }
        } catch (e) {
          reject('上传失败');
        }
      },
      fail: (err) => {
        reject('上传失败：' + err.errMsg);
      }
    });
  });
};

// 上传多张图片
export const uploadMultipleImages = async (filePaths) => {
  if (!Array.isArray(filePaths) || filePaths.length === 0) {
    return [];
  }
  
  try {
    const uploadPromises = filePaths.map(filePath => uploadImage(filePath));
    const results = await Promise.all(uploadPromises);
    return results;
  } catch (error) {
    console.error('批量上传图片失败:', error);
    throw new Error('批量上传图片失败');
  }
};

// 拜访登记接口
export const followProject = async (params = {}) => {
  return http.post('project/followProject', params)
}

// 获取中文地址
export const getAddress = async (params = {}) => {
  return http.post('project/getAddress', params)
}

// 删除项目接口
export const delProject = async (params = {}) => {
  return http.post('project/delProject', params)
}

// 获取审核流程
export const getAuditFlow = async (params = {}) => {
  return http.post('audit/getFlow', params)
}

// 提交审核操作
export const submitAudit = async (params = {}) => {
  return http.post('audit/submit', params)
}

// 财务相关接口
// 获取财务列表
export const getFinanceList = async (params = {}) => {
  return http.post('project/getFinancetList', params)
}

// 新增财务记录
export const addFinanceRecord = async (params = {}) => {
  return http.post('finance/add', params)
}

// 编辑财务记录
export const editFinanceRecord = async (params = {}) => {
  return http.post('finance/edit', params)
}

// 删除财务记录
export const deleteFinanceRecord = async (params = {}) => {
  return http.post('finance/delete', params)
}

// 生产相关接口
// 获取生产列表
export const getProductionList = async (params = {}) => {
  return http.post('production/getList', params)
}

// 新增生产项目
export const addProductionProject = async (params = {}) => {
  return http.post('production/add', params)
}

// 编辑生产项目
export const editProductionProject = async (params = {}) => {
  return http.post('production/edit', params)
}

// 完成生产项目
export const completeProductionProject = async (params = {}) => {
  return http.post('production/complete', params)
}

// 删除生产项目
export const deleteProductionProject = async (params = {}) => {
  return http.post('production/delete', params)
}

// 财务相关接口
export const getFinanceProjectList = async (params = {}) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: 'https://data.web1995.top/api/project/getProjectList',
      method: 'POST',
      data: {
        ...params,
        finance_id: uni.getStorageSync('user-id') || 1 // 获取当前财务人员ID
      },
      header: { 'token': uni.getStorageSync('sc-token') || '' },
      success: (res) => {
        // 确保返回的数据结构一致
        if (res.statusCode === 200 && res.data) {
          resolve(res.data);
        } else {
          reject(new Error('请求失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

export const getFinanceProjectDetail = async (id) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: 'https://data.web1995.top/api/project/getProjectDetail',
      method: 'POST',
      data: { id },
      header: { 'token': uni.getStorageSync('sc-token') || '' },
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

export const updateFinanceProject = async (params) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: 'https://data.web1995.top/api/project/editProject',
      method: 'POST',
      data: params,
      header: { 'token': uni.getStorageSync('sc-token') || '' },
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

export const addPaymentRecord = async (params) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: 'https://data.web1995.top/api/finance/addPaymentRecord',
      method: 'POST',
      data: params,
      header: { 'token': uni.getStorageSync('sc-token') || '' },
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};


// 获取项目列表
export const getProjectStatusList = async (params = {}) => {
  return http.post('project/getProjectStatusList', params)
}
